package pkg

import (
	"github.com/google/uuid"
	capi "github.com/hashicorp/consul/api"
)

type Consul struct {
	Client *capi.Client
}

func NewConsul(addr string) Consul {
	cfg := capi.DefaultConfig()
	cfg.Address = addr
	client, err := capi.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	return Consul{Client: client}
}

type ServiceConfig struct {
	Name    string
	Tags    []string
	Port    int
	Address string
}

func (c *Consul) Register(cfg ServiceConfig) error {
	service := &capi.AgentServiceRegistration{
		ID:      uuid.NewString(),
		Name:    cfg.Name,
		Tags:    cfg.Tags,
		Port:    cfg.Port,
		Address: cfg.Address,
	}
	return c.Client.Agent().ServiceRegister(service)
}
